clear all

use "$root\data\use.dta", replace


xtset fips year
//drop *38c*


**Main results with wetness controls
foreach crop in corn soybeans {
	local c_name = strproper("`crop'")

	preserve
	local c = cond("`crop'" == "corn", "c", "s")
	rename `c'_d*_* d*_*
	rename `c'_tot_* tot_*
	rename `c'_pdsi_w_* pdsi_w_*
	rename `c'_pmdi_w_* pmdi_w_*
	rename `c'_tbin*_* tbin*_*
	rename `c'_prec_* prec_*
	rename `crop'_production00 production00
	replace `crop'_price=log(`crop'_price)

**## Planned planting acres
	reghdfe ln`crop'_pplanted d*_b d*_p pdsi_w_b tbin*b pdsi_w_p tbin*p `crop'_price if irrigated_ratio<0.1 & N_year_`crop'>10 & lng>-100, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
	estimate store `crop'_pplanted_w

**## Planting ratio	
	reghdfe `crop'_plratio d*_b d*_p pdsi_w_b tbin*b pdsi_w_p tbin*p `crop'_price if irrigated_ratio<0.1 & N_year_`crop'>10 & lng>-100, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
	estimate store `crop'_plratio_w

**## Harvest ratio
	reghdfe `crop'_hratio d*_b d*_p d*_g pdsi_w_b tbin*b pdsi_w_p tbin*p pdsi_w_g tbin*g `crop'_price if irrigated_ratio<0.1 & N_year_`crop'>10 & lng>-100, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
	estimate store `crop'_hratio_w

**## Yield
	reghdfe ln`crop'_yield d*_b d*_p d*_g pdsi_w_b tbin*b pdsi_w_p tbin*p pdsi_w_g tbin*g `crop'_price if irrigated_ratio<0.1 & N_year_`crop'>10 & lng>-100, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
	estimate store `crop'_yield_w

**## Production
	reghdfe ln`crop'_production d*_b d*_p d*_g pdsi_w_b tbin*b pdsi_w_p tbin*p pdsi_w_g tbin*g `crop'_price if irrigated_ratio<0.1 & N_year_`crop'>10 & lng>-100, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
	estimate store `crop'_production_w


restore
}



**# With current price
foreach crop in corn soybeans {
	local c_name = strproper("`crop'")

	preserve
	local c = cond("`crop'" == "corn", "c", "s")
	rename `c'_d*_* d*_*
	rename `c'_tot_* tot_*
	rename `c'_pdsi_w_* pdsi_w_*
	rename `c'_pmdi_w_* pmdi_w_*
	rename `c'_tbin*_* tbin*_*
	rename `c'_prec_* prec_*
	rename `crop'_production00 production00
	replace `crop'_price=log(`crop'_price)

**## Planned planting acres
	reghdfe ln`crop'_pplanted d*_b d*_p pdsi_w_b tbin*b pdsi_w_p tbin*p `crop'_price if irrigated_ratio<0.1 & N_year_`crop'>10 & lng>-100, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
	estimate store `crop'_pplanted_p

**## Planting ratio	
	reghdfe `crop'_plratio d*_b d*_p pdsi_w_b tbin*b pdsi_w_p tbin*p `crop'_price if irrigated_ratio<0.1 & N_year_`crop'>10 & lng>-100, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
	estimate store `crop'_plratio_p

**## Harvest ratio
	reghdfe `crop'_hratio d*_b d*_p d*_g pdsi_w_b tbin*b pdsi_w_p tbin*p pdsi_w_g tbin*g `crop'_price if irrigated_ratio<0.1 & N_year_`crop'>10 & lng>-100, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
	estimate store `crop'_hratio_p

**## Yield
	reghdfe ln`crop'_yield d*_b d*_p d*_g pdsi_w_b tbin*b pdsi_w_p tbin*p pdsi_w_g tbin*g `crop'_price if irrigated_ratio<0.1 & N_year_`crop'>10 & lng>-100, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
	estimate store `crop'_yield_p

**## Production
	reghdfe ln`crop'_production d*_b d*_p d*_g pdsi_w_b tbin*b pdsi_w_p tbin*p pdsi_w_g tbin*g `crop'_price if irrigated_ratio<0.1 & N_year_`crop'>10 & lng>-100, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
	estimate store `crop'_production_p


restore
}


**# With lag price
foreach crop in corn soybeans {
	local c_name = strproper("`crop'")

	preserve
	local c = cond("`crop'" == "corn", "c", "s")
	rename `c'_d*_* d*_*
	rename `c'_tot_* tot_*
	rename `c'_pdsi_w_* pdsi_w_*
	rename `c'_pmdi_w_* pmdi_w_*
	rename `c'_tbin*_* tbin*_*
	rename `c'_prec_* prec_*
	rename `crop'_production00 production00
	
	replace `crop'_price=log(`crop'_price)

**## Planned planting acres
	reghdfe ln`crop'_pplanted d*_b d*_p pdsi_w_b tbin*b pdsi_w_p tbin*p l.`crop'_price if irrigated_ratio<0.1 & N_year_`crop'>10 & lng>-100, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
	estimate store `crop'_pplanted_lp

**## Planting ratio	
	reghdfe `crop'_plratio d*_b d*_p pdsi_w_b tbin*b pdsi_w_p tbin*p l.`crop'_price if irrigated_ratio<0.1 & N_year_`crop'>10 & lng>-100, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
	estimate store `crop'_plratio_lp

**## Harvest ratio
	reghdfe `crop'_hratio d*_b d*_p d*_g pdsi_w_b tbin*b pdsi_w_p tbin*p pdsi_w_g tbin*g l.`crop'_price if irrigated_ratio<0.1 & N_year_`crop'>10 & lng>-100, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
	estimate store `crop'_hratio_lp

**## Yield
	reghdfe ln`crop'_yield d*_b d*_p d*_g pdsi_w_b tbin*b pdsi_w_p tbin*p pdsi_w_g tbin*g l.`crop'_price if irrigated_ratio<0.1 & N_year_`crop'>10 & lng>-100, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
	estimate store `crop'_yield_lp

**## Production
	reghdfe ln`crop'_production d*_b d*_p d*_g pdsi_w_b tbin*b pdsi_w_p tbin*p pdsi_w_g tbin*g l.`crop'_price if irrigated_ratio<0.1 & N_year_`crop'>10 & lng>-100, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
	estimate store `crop'_production_lp


restore
}


**## Combined plots
coefplot (corn_pplanted_w, offset(-0.25)) /// 
	(corn_pplanted_p, offset(0) msymbol(T)) ///
	(corn_pplanted_lp, offset(0.25) msymbol(S)), bylabel(Planned Acres, Corn)  || ///
	(soybeans_pplanted_w, offset(-0.25)) /// 
	(soybeans_pplanted_p, offset(0)  msymbol(T)) ///
	(soybeans_pplanted_lp, offset(0.25) msymbol(S)), bylabel(Planned Acres, Soybeans) || ///
	(corn_plratio_w, offset(-0.25)) /// 
	(corn_plratio_p, offset(0) msymbol(T)) ///
	(corn_plratio_lp, offset(0.25) msymbol(S)), bylabel(Planted Ratio, Corn) || ///
	(soybeans_plratio_w, offset(-0.25)) /// 
	(soybeans_plratio_p, offset(0) msymbol(T)) ///
	(soybeans_plratio_lp, offset(0.25) msymbol(S)), bylabel(Planted Ratio, Soybeans) || ///
	(corn_hratio_w, offset(-0.25)) /// 
	(corn_hratio_p, offset(0) msymbol(T)) ///
	(corn_hratio_lp, offset(0.25) msymbol(S)), bylabel(Harvest Ratio, Corn) || ///
	(soybeans_hratio_w, offset(-0.25)) /// 
	(soybeans_hratio_p, offset(0) msymbol(T)) ///
	(soybeans_hratio_lp, offset(0.25) msymbol(S)), bylabel(Harvest Ratio, Soybeans) || ///
	(corn_yield_w, label(Main Results) offset(-0.25)) /// 
	(corn_yield_p, offset(0) msymbol(T)) /// 
	(corn_yield_lp, offset(0.25) msymbol(S)), bylabel(Yield, Corn) || ///
	(soybeans_yield_w, label(Main Results) offset(-0.25)) /// 
	(soybeans_yield_p, offset(0) msymbol(T)) /// 
	(soybeans_yield_lp, offset(0.25) msymbol(S)), bylabel(Yield, Soybeans) || ///
	(corn_production_w, label(Main Results) offset(-0.25)) /// 
	(corn_production_p, offset(0) msymbol(T)) /// 
	(corn_production_lp, offset(0.25) msymbol(S)), bylabel(Production, Corn) || ///
	(soybeans_production_w, label(Main Results) offset(-0.25)) /// 
	(soybeans_production_p, offset(0) msymbol(T)) /// 
	(soybeans_production_lp, offset(0.25) msymbol(S)), bylabel(Production, Soybeans) ||, ///
	mcolor(black) mfcolor(white) msize(small) mlwidth(thin) ciopts(color(black)  lwidth(thin)) ///
	vertical keep(d*_b d*_p d*_g) yline(0, lcolor(gray) lwidth(0.2)) legend(rows(1) size(vsmall) region(lstyle(none)) colgap(20)) /// 
	byopts(yrescale compact cols(2) graphregion(fcolor(white) margin(l r+3 b-5 t-5)))  ///
	groups(d*_b = Pre-planting d*_p = Planting d*_g = Growing) ylabel(, labsize(vsmall) angle(45)) ///
	xlabel(1 "D0" 2 "D1" 3 "D2" 4 "D3" 6 "D0" 7 "D1" 8 "D2" 9 "D3" 11 "D0" 12 "D1" 13 "D2" 14 "D3", labsize(vsmall) angle(45)) ///
	plotlabels("Main" "Current" "Lagged") subtitle(, fcolor(white) bmargin(top) bcolor(white) size(small))
	graph export "$root/results_replication/appendix_figures/a5_price.png", as(png) name("Graph") width(3200) height(3600) replace



